<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchFormData" class="demo-form-inline" size="mini">
            	            	        <el-form-item
      	        label="社区消息id"
              >
      	<el-input v-model="searchFormData.communityId" :clearable="true" placeholder="社区消息id" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="用户id"
              >
      	<el-input v-model="searchFormData.userId" :clearable="true" placeholder="用户id" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="头像地址"
              >
      	<el-input v-model="searchFormData.avatarUrl" :clearable="true" placeholder="头像地址" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="用户名"
              >
      	<el-input v-model="searchFormData.username" :clearable="true" placeholder="用户名" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="父级评论id"
              >
      	<el-input v-model="searchFormData.parentId" :clearable="true" placeholder="父级评论id" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="评论内容"
              >
      	<el-input v-model="searchFormData.content" :clearable="true" placeholder="评论内容" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="点赞数"
              >
      	<el-input v-model="searchFormData.likeCount" :clearable="true" placeholder="点赞数" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="是否启用，1表示启用，0表示禁用"
              >
      	<el-input v-model="searchFormData.enabled" :clearable="true" placeholder="是否启用，1表示启用，0表示禁用" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="数据创建时间"
              >
      	<el-input v-model="searchFormData.createTime" :clearable="true" placeholder="数据创建时间" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="数据最后修改时间"
              >
      	<el-input v-model="searchFormData.updateTime" :clearable="true" placeholder="数据最后修改时间" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="数据创建者"
              >
      	<el-input v-model="searchFormData.creator" :clearable="true" placeholder="数据创建者" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="数据更新者"
              >
      	<el-input v-model="searchFormData.updater" :clearable="true" placeholder="数据更新者" style="width: 250px;" />
      </el-form-item>
                    <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="loadTable">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" size="mini" icon="el-icon-plus" style="margin-bottom: 10px;" @click="onAdd">新增</el-button>
    <el-table
      :data="pageInfo.rows"
      border
      highlight-current-row
    >
          <el-table-column
        prop="id"
                label="id"
              />
           <el-table-column
        prop="communityId"
                label="社区消息id"
              />
           <el-table-column
        prop="userId"
                label="用户id"
              />
           <el-table-column
        prop="avatarUrl"
                label="头像地址"
              />
           <el-table-column
        prop="username"
                label="用户名"
              />
           <el-table-column
        prop="parentId"
                label="父级评论id"
              />
           <el-table-column
        prop="content"
                label="评论内容"
              />
           <el-table-column
        prop="likeCount"
                label="点赞数"
              />
           <el-table-column
        prop="enabled"
                label="是否启用，1表示启用，0表示禁用"
              />
           <el-table-column
        prop="createTime"
                label="数据创建时间"
              />
           <el-table-column
        prop="updateTime"
                label="数据最后修改时间"
              />
           <el-table-column
        prop="creator"
                label="数据创建者"
              />
           <el-table-column
        prop="updater"
                label="数据更新者"
              />
          <el-table-column>
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="onTableUpdate(scope.row)">修改</el-button>
          <el-button type="text" size="mini" @click="onTableDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      style="margin-top: 5px"
      :current-page="searchFormData.pageIndex"
      :page-size="searchFormData.pageSize"
      :page-sizes="[5, 10, 20, 40]"
      :total="pageInfo.total"
      layout="total, sizes, prev, pager, next"
      @size-change="onSizeChange"
      @current-change="onPageIndexChange"
    />
    <!--dialog-->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      @close="resetForm('dialogForm')"
    >
      <el-form
        ref="dialogForm"
        :rules="dialogFormRules"
        :model="dialogFormData"
        label-width="120px"
        size="mini"
      >
            	            	        <el-form-item
      	prop="communityId"
                label="社区消息id"
               >
          <el-input v-model="dialogFormData.communityId" />
        </el-form-item>
                    	        <el-form-item
      	prop="userId"
                label="用户id"
               >
          <el-input v-model="dialogFormData.userId" />
        </el-form-item>
                    	        <el-form-item
      	prop="avatarUrl"
                label="头像地址"
               >
          <el-input v-model="dialogFormData.avatarUrl" />
        </el-form-item>
                    	        <el-form-item
      	prop="username"
                label="用户名"
               >
          <el-input v-model="dialogFormData.username" />
        </el-form-item>
                    	        <el-form-item
      	prop="parentId"
                label="父级评论id"
               >
          <el-input v-model="dialogFormData.parentId" />
        </el-form-item>
                    	        <el-form-item
      	prop="content"
                label="评论内容"
               >
          <el-input v-model="dialogFormData.content" />
        </el-form-item>
                    	        <el-form-item
      	prop="likeCount"
                label="点赞数"
               >
          <el-input v-model="dialogFormData.likeCount" />
        </el-form-item>
                    	        <el-form-item
      	prop="enabled"
                label="是否启用，1表示启用，0表示禁用"
               >
          <el-input v-model="dialogFormData.enabled" />
        </el-form-item>
                    	        <el-form-item
      	prop="createTime"
                label="数据创建时间"
               >
          <el-input v-model="dialogFormData.createTime" />
        </el-form-item>
                    	        <el-form-item
      	prop="updateTime"
                label="数据最后修改时间"
               >
          <el-input v-model="dialogFormData.updateTime" />
        </el-form-item>
                    	        <el-form-item
      	prop="creator"
                label="数据创建者"
               >
          <el-input v-model="dialogFormData.creator" />
        </el-form-item>
                    	        <el-form-item
      	prop="updater"
                label="数据更新者"
               >
          <el-input v-model="dialogFormData.updater" />
        </el-form-item>
                    </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onDialogSave">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
// 创建axios实例
const client = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
  timeout: 60000 // 请求超时时间,60秒
})
export default {
  data() {    
    return {
      searchFormData: {
                id: '',
                communityId: '',
                userId: '',
                avatarUrl: '',
                username: '',
                parentId: '',
                content: '',
                likeCount: '',
                enabled: '',
                createTime: '',
                updateTime: '',
                creator: '',
                updater: '',
                pageIndex: 1,
        pageSize: 10
      },
      pageInfo: {
        rows: [],
        total: 0
      },
      dialogVisible: false,
      dialogTitle: '',
      dialogFormData: {
                 id: ''
                , communityId: ''
                , userId: ''
                , avatarUrl: ''
                , username: ''
                , parentId: ''
                , content: ''
                , likeCount: ''
                , enabled: ''
                , createTime: ''
                , updateTime: ''
                , creator: ''
                , updater: ''
              },
      dialogFormRules: {        
                 id: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , communityId: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , userId: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , avatarUrl: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , username: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , parentId: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , content: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , likeCount: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , enabled: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , createTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , updateTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , creator: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , updater: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
              }
    }
  },
  created() {
    this.loadTable()
  },
  methods: {
    loadTable() {
      this.post('/communityComment/list', this.searchFormData, resp => {
        this.pageInfo.rows = resp.data
      })
    },
    onTableUpdate(row) {
      this.dialogTitle = '修改'
      this.dialogVisible = true
      this.$nextTick(() => {
        Object.assign(this.dialogFormData, row)
      })
    },
    onTableDelete(row) {
      this.confirm(`确认要删除该记录吗？`, function(done) {
        const data = {
          id: row.id
        }
        this.post('/communityComment/del', data, () => {
          done()
          this.tip('删除成功')
          this.loadTable()
        })
      })
    },
    onDialogSave() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          const uri = this.dialogFormData.id ? '/communityComment/update' : '/communityComment/add'
          this.post(uri, this.dialogFormData, () => {
            this.dialogVisible = false
            this.loadTable()
          })
        }
      })
    },
    onSizeChange(size) {
      this.searchFormData.pageSize = size
      this.loadTable()
    },
    onAdd() {
      this.dialogTitle = '新增'
      this.dialogVisible = true
      this.dialogFormData.id = 0
    },
    onPageIndexChange(pageIndex) {
      this.searchFormData.pageIndex = pageIndex
      this.loadTable()
    },
    /**
     * 请求接口
     * @param uri uri
     * @param data 请求数据
     * @param callback 成功时回调
     * @param errorCallback 错误时回调
     */
    post(uri, data, callback, errorCallback) {
      const that = this
      client.post(uri, data).then(function(response) {
        const resp = response.data
        const code = resp.code
        if (code === '0') { // 成功
          callback && callback.call(that, resp)
        } else {
          that.$message.error(resp.msg)
        }
      }).catch(function(error) {
        console.error(error)
        errorCallback && errorCallback(error)
        that.$message.error(error.message)
      })
    }
  }
}
</script>